//
//  RefundDetailViewController.m
//  TryShopping
//
//  Created by panquanReabam.com on 16/6/20.
//  Copyright © 2016年 RuiBen. All rights reserved.
//

#import "RefundDetailViewController.h"
#import "UIViewExt.h"
#import "HttpRequest.h"
#import "JSONKit.h"
#import "UIImageView+WebCache.h"
#import "RDVTabBarController.h"
#import "GoodsView.h"
@interface RefundDetailViewController ()<MBProgressHUDDelegate,UIGestureRecognizerDelegate,UINavigationControllerDelegate,UITextFieldDelegate,UITextViewDelegate,UISearchBarDelegate,UIScrollViewDelegate>
@end
@implementation RefundDetailViewController
{
    
    GoodsView*shangpingView;
    UIScrollView*MYmainScroll;
    NSMutableArray* itemsArray;

}
-(void)BackAction:(UIButton *)sender{
    
    NSUserDefaults*pushJudge = [NSUserDefaults standardUserDefaults];
    if([[pushJudge objectForKey:@"push"]isEqualToString:@"push"]) {
        NSUserDefaults * pushJudge = [NSUserDefaults standardUserDefaults];
        [pushJudge setObject:@""forKey:@"push"];
        [pushJudge synchronize];//记得立即同步
        [self dismissViewControllerAnimated:YES completion:nil];
        
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
}
- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    self.navigationController.delegate = nil;
    
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [[self rdv_tabBarController] setTabBarHidden:YES];
    self.navigationController.navigationBarHidden=NO;
    self.navigationController.navigationBar.barStyle = UIBaselineAdjustmentNone;
    UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 43,ScreenWidth, 1)];
    [lineLabel setBackgroundColor:[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1.0]];
    [self.navigationController.navigationBar addSubview:lineLabel];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setBarTintColor:Color];
    UIImage* backImage = [UIImage imageNamed:@"fanhui"];
    CGRect backframe = CGRectMake(0,0,30,30);
    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    backButton.titleLabel.textAlignment=NSTextAlignmentLeft;
    [backButton addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    self.navigationController.delegate = self;
    
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@""] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage =[[UIImage alloc] init];
    UILabel* titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 0,40, 23)];
    titleLabel.text=@"退货详情";
    titleLabel.font=[UIFont systemFontOfSize: 17];
    self.navigationItem.titleView=titleLabel;
    
}

- (void)viewDidLoad
{
    MYmainScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0, ScreenWidth, ScreenHeight)];
    [MYmainScroll setBackgroundColor: zuiqianhuiColor];
    MYmainScroll.delegate = self;
    MYmainScroll.userInteractionEnabled = YES;
    MYmainScroll.scrollEnabled = YES;
    MYmainScroll.showsVerticalScrollIndicator = FALSE;
    MYmainScroll.showsHorizontalScrollIndicator = FALSE;
    [self.view addSubview:MYmainScroll];

    [super viewDidLoad];
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token = [user objectForKey:@"tokenId"];
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setValue:Token forKey:@"tokenId"];
    [param setValue:self.orderId forKey:@"refundId"];
    [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
    [HttpRequest requestData:@"Business/newRefund/Detail" params:param success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            NSLog(@"====%@",severDataDict.dataDict);
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [Global hideProgressHUDInternal:self.view];
                
                UIView*view1=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 70)];
                view1.backgroundColor=[UIColor whiteColor];
                [MYmainScroll addSubview:view1];
                
                NSString *s2 =[[severDataDict.dataDict objectForKey:@"refund"]objectForKey:@"refundNo"];
                UIFont *font2 = [UIFont systemFontOfSize:17];
                CGSize size2 = CGSizeMake(ScreenWidth-60, 2000);
                CGSize labelsize2 = [s2 sizeWithFont:font2 constrainedToSize:size2 lineBreakMode:NSLineBreakByCharWrapping];
                UILabel* refundNo=[[UILabel alloc]initWithFrame:CGRectMake(15, 15, labelsize2.width+5, 17)];
                refundNo.text=s2;
                refundNo.textColor=[UIColor blackColor];
                refundNo.font=[UIFont systemFontOfSize:17];
                [view1 addSubview:refundNo];
                
                
                NSString *s =[[severDataDict.dataDict objectForKey:@"refund"]objectForKey:@"rgtypeName"];
                UIFont *font = [UIFont systemFontOfSize:13];
                CGSize size = CGSizeMake(ScreenWidth-60, 2000);
                CGSize labelsize = [s sizeWithFont:font constrainedToSize:size lineBreakMode:NSLineBreakByCharWrapping];
                UILabel*rgtypeName=[[UILabel alloc]initWithFrame:CGRectMake(refundNo.right+5, 15, labelsize.width, 14)];
                rgtypeName.backgroundColor=luseColor;
                [rgtypeName setTextColor:[UIColor whiteColor]];
                rgtypeName.text=[NSString stringWithFormat:@"%@",[[severDataDict.dataDict objectForKey:@"refund"]objectForKey:@"rgtypeName"]];
                rgtypeName.font= [UIFont systemFontOfSize:12];
                rgtypeName.textAlignment=NSTextAlignmentCenter;
                [view1 addSubview:rgtypeName];
                
                UILabel*refundStatusName=[[UILabel alloc]initWithFrame:CGRectMake(rgtypeName.right+5, 15, 230, 14)];
                [refundStatusName setTextColor:luseColor];
                refundStatusName.text=[NSString stringWithFormat:@"%@",[[severDataDict.dataDict objectForKey:@"refund"]objectForKey:@"refundStatusName"]];
                refundStatusName.font= [UIFont systemFontOfSize:contenfot];
                [view1 addSubview:refundStatusName];
                UILabel *line1=[[UILabel alloc] initWithFrame:CGRectMake(0, 70, ScreenWidth, 0.40)];
                line1.backgroundColor=qianhuiColor;
                [view1 addSubview:line1];
                UILabel* duogou=[[UILabel alloc]initWithFrame:CGRectMake(15, 85/2, 33, contenfot)];
                duogou.text=[NSString stringWithFormat:@"来源"];
                duogou.textColor=zhonghuiColor;
                duogou.font=[UIFont systemFontOfSize:contenfot];
                [view1 addSubview:duogou];
                NSString *createName1 =[[severDataDict.dataDict objectForKey:@"refund"]objectForKey:@"source"];
                UIFont *createNamefont1 = [UIFont systemFontOfSize:contenfot];
                CGSize createNamesize1 = CGSizeMake(ScreenWidth-60, 2000);
                CGSize createNamelabelsize1 = [createName1 sizeWithFont:createNamefont1 constrainedToSize:createNamesize1 lineBreakMode:NSLineBreakByCharWrapping];
                
                UIButton*daogouname=[[UIButton alloc]initWithFrame:CGRectMake(duogou.right, 85/2,createNamelabelsize1.width+15,contenfot)];
                [daogouname setTitle:createName1 forState:UIControlStateNormal];
                daogouname.titleLabel.font=[UIFont systemFontOfSize:contenfot];
                daogouname.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft;
                [daogouname setTitleColor:shenhuiColor forState:UIControlStateNormal];
                [view1 addSubview:daogouname];
                
                UILabel* orderDate=[[UILabel alloc]initWithFrame:CGRectMake(daogouname.right, 85/2, 33, contenfot)];
                orderDate.text=@"时间";
                orderDate.textColor=zhonghuiColor;
                orderDate.font=[UIFont systemFontOfSize:contenfot];
                [view1 addSubview:orderDate];
//                NSRange range={0,10};
                NSString* string =[[severDataDict.dataDict  objectForKey:@"refund"]objectForKey:@"createDate"] ;
                UILabel* orderDate1=[[UILabel alloc]initWithFrame:CGRectMake(orderDate.right, 85/2, 180, contenfot)];
                orderDate1.text=string;
                orderDate1.textColor=shenhuiColor;
                orderDate1.font=[UIFont systemFontOfSize:contenfot];
                [view1 addSubview:orderDate1];
                
                
                UIView* view2=[[UIView alloc]initWithFrame:CGRectMake(0, view1.bottom+10, ScreenWidth, 50)];
                view2.backgroundColor=[UIColor whiteColor];
                view2.layer.borderWidth = 0.40;
                view2.layer.borderColor = qianhuiColor.CGColor;
                [MYmainScroll addSubview:view2];
                
                
                NSDictionary *value = [severDataDict.dataDict  objectForKey:@"member"];
            if (value.count==0)
                {
                    UILabel*memberLabel=[[UILabel alloc]initWithFrame:CGRectMake(15, 0, 100, 50)];
                    memberLabel.text=@"零售会员";
                    memberLabel.textColor=qianheiColor;
                    memberLabel.font=[UIFont systemFontOfSize:15];
                    [view2 addSubview:memberLabel];
                }else{
                NSString *s3 = [NSString stringWithFormat:@"%@(%@)",   [[severDataDict.dataDict  objectForKey:@"member"]objectForKey:@"memberName"], [[severDataDict.dataDict  objectForKey:@"member"]objectForKey:@"phone"]];
                UIFont *font3 = [UIFont systemFontOfSize:15];
                CGSize size3 = CGSizeMake(320, 2000);
                CGSize labelsize3 = [s3 sizeWithFont:font3 constrainedToSize:size3 lineBreakMode:NSLineBreakByCharWrapping];
                UILabel*memberLabel=[[UILabel alloc]initWithFrame:CGRectMake(15, 0, labelsize3.width+3, 50)];
                memberLabel.text=s3;
                memberLabel.textColor=qianheiColor;
                memberLabel.font=[UIFont systemFontOfSize:15];
                [view2 addSubview:memberLabel];
                
                UIImageView*Seximage=[[UIImageView alloc]initWithFrame:CGRectMake(memberLabel.right+5, 14.5, 20, 20)];
                    if (![ [[severDataDict.dataDict  objectForKey:@"member"]objectForKey:@"sex"] isEqual:[NSNull null]]) {
                        
                        if ([ [[severDataDict.dataDict  objectForKey:@"member"]objectForKey:@"sex"] isEqualToString:@"男"]) {
                            
                            
                            Seximage.image=[UIImage imageNamed:@"nan"];
                            
                        }else{
                            
                            Seximage.image=[UIImage imageNamed:@"nv"];
                            
                        }
                        [view2 addSubview:Seximage];
                    }
                    NSString *s4 =[[severDataDict.dataDict  objectForKey:@"member"]objectForKey:@"gradeName"] ;
                    UIFont *font4 = [UIFont systemFontOfSize:14];
                    CGSize size4 = CGSizeMake(320, 2000);
                    CGSize labelsize4 = [s4 sizeWithFont:font4 constrainedToSize:size4 lineBreakMode:NSLineBreakByCharWrapping];
               
                UILabel*grade=[[UILabel alloc] initWithFrame:CGRectMake(Seximage.right+5,17.5,labelsize4.width, 14)];
                    grade.text=s4;
                    grade.textColor=[UIColor whiteColor];
                    grade.textAlignment=NSTextAlignmentCenter;
                    grade.font=[UIFont systemFontOfSize:13];
                    grade.backgroundColor=[UIColor colorWithRed:209/255.0 green:170/255.0 blue:56/255.0 alpha:1.0];
                    [view2 addSubview:grade];

                }
                UIView* view3=[[UIView alloc]initWithFrame:CGRectMake(0, view2.bottom+10, ScreenWidth, 45)];
                view3.backgroundColor=[UIColor whiteColor];
                [MYmainScroll addSubview:view3];
                UILabel *lineL=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 0.40)];
                lineL.backgroundColor=qianhuiColor;
                [view3 addSubview:lineL];
                
                UILabel*shangpingxinxi=[[UILabel alloc]initWithFrame:CGRectMake(15, 0, 80, 45)];
                shangpingxinxi.text=@"商品信息";
                shangpingxinxi.textColor=qianheiColor;
                shangpingxinxi.font=[UIFont systemFontOfSize:15];
                [view3 addSubview:shangpingxinxi];
                
                
                NSNumber*Total = [[severDataDict.dataDict objectForKey:@"refund"]objectForKey:@"totalMoney"];
                
                
                NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
                [numberFormatter setPositiveFormat:@"###,##0.00;"];
                NSString *formattedNumberString = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:Total.doubleValue]];
                UILabel* weightke=[[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth-250, 0, 230, 45)];
                weightke.text=[NSString  stringWithFormat:@"共 %@ 件，  ￥%@",[[severDataDict.dataDict objectForKey:@"refund"]objectForKey:@"totalQuantity"], formattedNumberString];
                weightke.textColor = [UIColor colorWithRed:5.00/255.0 green:18.0/255.0 blue:40.00/255.0 alpha:1];
                weightke.textAlignment = NSTextAlignmentRight;
                weightke.font = [UIFont systemFontOfSize:16];
                NSMutableAttributedString *AttributedStr2 = [[NSMutableAttributedString alloc]initWithString:[NSString  stringWithFormat:@"共 %@ 件，  ￥%@",[[severDataDict.dataDict objectForKey:@"refund"]objectForKey:@"totalQuantity"], formattedNumberString]];
                [AttributedStr2 addAttribute:NSFontAttributeName
                 
                                       value:[UIFont systemFontOfSize:13.0]
                 
                                       range:NSMakeRange(0, 8)];
                
                [AttributedStr2 addAttribute:NSForegroundColorAttributeName
                 
                                       value:zhonghuiColor
                 
                                       range:NSMakeRange(0, 8)];
                
                
                weightke.attributedText = AttributedStr2;
                [view3 addSubview:weightke];
                UILabel *lineLabel1=[[UILabel alloc] initWithFrame:CGRectMake(10, 45-0.40, ScreenWidth-20, 0.40)];
                lineLabel1.backgroundColor=qianhuiColor;
                [view3 addSubview:lineLabel1];
                itemsArray=[severDataDict.dataDict  objectForKey:@"items"];
    
                if (itemsArray.count>0) {
                    
                    for (int i = 0; i<itemsArray.count; i++) {
                        
                        shangpingView = [[GoodsView alloc]initWithFrame:CGRectMake(0, view3.bottom+(275/2*i), ScreenWidth, 275/2)];
                        shangpingView.backgroundColor=[UIColor whiteColor];
                        [shangpingView.headImageFull setImageWithURL:[[itemsArray objectAtIndex:i] objectForKey:@"imageUrlFull"] placeholderImage:[UIImage imageNamed:@"zhengfangxing"]];
                        [shangpingView.headImageFull.layer setCornerRadius:6.0];
                        [shangpingView.headImageFull.layer setBorderWidth:0.5];
                        [shangpingView.headImageFull.layer setMasksToBounds:YES];
                        CGColorSpaceRef colorSpace2 = CGColorSpaceCreateDeviceRGB();
                        CGColorRef colorref2 = CGColorCreate(colorSpace2,(CGFloat[]){ 0.93, 0.93, 0.93, 0.93 });
                        [shangpingView.headImageFull.layer setBorderColor:colorref2];//边框颜色
                        shangpingView.itemName.text=[NSString stringWithFormat:@"%@",[[itemsArray objectAtIndex:i] objectForKey:@"itemName"]];
                        shangpingView.quantity.text=[NSString stringWithFormat:@"数量 %@",[[itemsArray objectAtIndex:i] objectForKey:@"quantity"]];
                        NSMutableAttributedString *AttributedStr1 = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"数量 %@",[[itemsArray objectAtIndex:i] objectForKey:@"quantity"]]];
                        
                        [AttributedStr1 addAttribute:NSFontAttributeName
                         
                                               value:[UIFont systemFontOfSize:12]
                         
                                               range:NSMakeRange(0, 3)];
                        
                        [AttributedStr1 addAttribute:NSForegroundColorAttributeName
                         
                                               value:zhonghuiColor
                         
                                               range:NSMakeRange(0, 3)];
                        
                        
                        shangpingView.quantity.attributedText = AttributedStr1;
                        
                        
                        NSNumber*Total =[[itemsArray objectAtIndex:i] objectForKey:@"salePrice"];
                        
                        NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
                        [numberFormatter setPositiveFormat:@"###,##0.00;"];
                        NSString *formattedNumberString = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:Total.doubleValue]];
                        shangpingView.dealPrice.text=[NSString stringWithFormat:@"¥ %@",formattedNumberString];
                        shangpingView.dealPrice.textColor=qianheiColor;
                        NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"¥ %@",formattedNumberString]];
                        
                        [AttributedStr addAttribute:NSFontAttributeName
                         
                                               value:[UIFont systemFontOfSize:13]
                         
                                               range:NSMakeRange(0, 1)];
                        
                        [AttributedStr addAttribute:NSForegroundColorAttributeName
                         
                                               value:shenhuiColor
                         
                                               range:NSMakeRange(0, 1)];
                        
                        
                        shangpingView.dealPrice.attributedText = AttributedStr;
                      
                        shangpingView.itemType.text=[NSString stringWithFormat:@"%@",[[itemsArray objectAtIndex:i] objectForKey:@"specName"]];
                        
                        UILabel*refundText= [[UILabel alloc]init];
                        refundText.frame=CGRectMake(ScreenWidth-200, 110, 185,13);
                        refundText.text=[NSString stringWithFormat:@"退货单价¥ %@",[[itemsArray objectAtIndex:i] objectForKey:@"refundMoney"]];
                        refundText.textColor=hongseColor;
                        refundText.textAlignment=NSTextAlignmentRight;
                        refundText.font=[UIFont systemFontOfSize:13];
                        NSMutableAttributedString *AttributedStr2 = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"退货单价 ¥ %@",[[itemsArray objectAtIndex:i] objectForKey:@"refundMoney"]]];
                        
                        [AttributedStr2 addAttribute:NSFontAttributeName
                         
                                              value:[UIFont systemFontOfSize:13]
                         
                                              range:NSMakeRange(0, 4)];
                        
                        [AttributedStr2 addAttribute:NSForegroundColorAttributeName
                         
                                              value:zhonghuiColor
                         
                                              range:NSMakeRange(0, 4)];
                        
                        
                        refundText.attributedText = AttributedStr2;

                        [shangpingView addSubview:refundText];

                        
                        if (i==itemsArray.count-1) {
                            UILabel *lineLabel1=[[UILabel alloc] initWithFrame:CGRectMake(0, 275/2-0.40, ScreenWidth, 0.40)];
                            lineLabel1.backgroundColor=qianhuiColor;
                            [shangpingView addSubview:lineLabel1];
                        }else{
                            UILabel *lineLabel1=[[UILabel alloc] initWithFrame:CGRectMake(10, 275/2-0.40, ScreenWidth-20, 0.40)];
                            lineLabel1.backgroundColor=qianhuiColor;
                            [shangpingView addSubview:lineLabel1];
                            
                            
                        }
                        
                        [MYmainScroll addSubview:shangpingView];
                    }
                    
                }else{
                    
                    
                    shangpingView = [[GoodsView alloc]initWithFrame:CGRectMake(0, view3.bottom, ScreenWidth, 120)];
                    shangpingView.itemName.text=@"无商品信息";
                    [MYmainScroll addSubview:shangpingView];
                    
                }
                UIView*view6=[[UIView alloc]initWithFrame:CGRectMake(0, shangpingView.bottom+10, ScreenWidth, 80)];
                view6.backgroundColor=[UIColor whiteColor];
                view6.layer.borderWidth = 0.5;
                view6.layer.borderColor = qianhuiColor.CGColor;
                [MYmainScroll addSubview:view6];
                
                UILabel*remarkLabel=[[UILabel alloc]initWithFrame:CGRectMake(15, 0, ScreenWidth*70/750, 40)];
                remarkLabel.text=@"备注";
                remarkLabel.font= [UIFont systemFontOfSize:contenfot];
                [remarkLabel setTextColor:qianheiColor];
                [view6 addSubview:remarkLabel];
                
                NSString *s1 = [[severDataDict.dataDict objectForKey:@"refund"]objectForKey:@"remark"];
                if (s1.length>0) {
                    
                }else{
                    s1=@"无";
                    
                }
                UIFont *font1 = [UIFont systemFontOfSize:contenfot];
                CGSize size1 = CGSizeMake(ScreenWidth-60, 2000);
                CGSize labelsize1 = [s1 sizeWithFont:font1 constrainedToSize:size1 lineBreakMode:NSLineBreakByCharWrapping];
                
                UILabel*remarkLabel1=[[UILabel alloc]initWithFrame:CGRectMake(15, remarkLabel.bottom, ScreenWidth-40, labelsize1.height+10)];
                remarkLabel1.text=s1;
                remarkLabel1.numberOfLines=0;
                remarkLabel1.font= [UIFont systemFontOfSize:contenfot];
                [remarkLabel1 setTextColor:shenhuiColor];
                [view6 addSubview:remarkLabel1];
                view6.frame=CGRectMake(0,shangpingView.bottom+10, ScreenWidth, labelsize1.height+70);
                MYmainScroll.contentSize = CGSizeMake(ScreenWidth, view6.bottom+100);

                }else{
                [Global hideProgressHUDInternal:self.view];
                NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                [Global showTextHudSub:requestResultStr view:self.view];
            }
        }
        else
        {
            [Global hideProgressHUDInternal:self.view];
            [Global showTextHudSub:severDataDict.resultStr view:self.view];
        }
        
    }];
}

@end
